package com.xuan.book.service;

import com.xuan.book.enums.BookStatusEnums;
import com.xuan.book.mapper.BookInfoMapper;
import com.xuan.book.model.BookInfo;
import com.xuan.book.model.PageRequest;
import com.xuan.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    /**
     * 根据数据层返回的结果，对数据进行处理
     * @return
     */
    @Autowired
    private BookInfoMapper bookInfoMapper;


    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        // 1. 查询记录的总数
        Integer count = bookInfoMapper.count();

        // 2. 查询当前页的记录
        List<BookInfo> bookInfos = bookInfoMapper.getListByPage(pageRequest);
        for(BookInfo bookInfo : bookInfos) {
            // 根据状态，设置描述
            bookInfo.setStateCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
        }

        return new PageResult<>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookById(Integer id) {
        return bookInfoMapper.queryBookInfoById(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }


    public boolean batchDeleteBook(int[] ids) {
        Integer result = bookInfoMapper.batchDeleteBook(ids);
        return result > 0;
    }
}
